package com.woniuxy.woniuhis.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.woniuhis.entity.Inpatient;
import com.woniuxy.woniuhis.entity.PayIn;
import com.woniuxy.woniuhis.service.PayInService;
import com.woniuxy.woniuhis.util.PayInDetails;
import com.woniuxy.woniuhis.util.ResponseInfo;
import com.woniuxy.woniuhis.util.ResponseJson;

@Controller
@RequestMapping("/payIn")
public class PayInController {
	@Autowired
	PayInService payInService;
	
//	@RequestMapping("/payInList")
//	@ResponseBody
//	/**
//	 * 患者订单展示
//	 */
//	public ResponseJson<Inpatient> payInList(Integer page,Integer limit) {
//		ResponseJson<Inpatient> responseJson=new ResponseJson<Inpatient>();
//		try {
//			Inpatient condition=new Inpatient();
//			List<Inpatient> payIns = payInService.findInpatientPayIn(condition, null, null);
//			responseJson.setCode(0);
//			responseJson.setCount(payIns.size());
//			responseJson.setData(payIns);
//			return responseJson;
//		} catch (Exception e) {
//			e.printStackTrace();
//			responseJson.setCode(500);
//			responseJson.setMsg("系统维护中");
//			return responseJson;
//		}
//	}
	
	@RequestMapping("/payInSearchList")
	@ResponseBody
	/**
	 * 查询患者订单
	 */
	public ResponseJson<Inpatient> payInSearchList(@RequestBody Inpatient inpatient) {
		System.out.println(inpatient);
		ResponseJson<Inpatient> responseJson=new ResponseJson<Inpatient>();
		Date begindate = null;
		Date enddate = null;
		try {
			if(inpatient.getInpatientFamlilyName()!=""&&inpatient.getInpatientFamlilyName()!=null) {
				String[] split = inpatient.getInpatientFamlilyName().split(" - ");
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				begindate = sdf.parse(split[0]);
				enddate = sdf.parse(split[1]);
			}
			List<Inpatient> payIns = payInService.findInpatientPayIn(inpatient, begindate, enddate);
			System.out.println(payIns);
			responseJson.setCode(0);
			responseJson.setCount(payIns.size());
			responseJson.setData(payIns);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(500);
			responseJson.setMsg("系统维护中");
			return responseJson;
		}
	}
	
//	@RequestMapping("/payInDetails")
//	@ResponseBody
//	/**
//	 * 订单展示
//	 */
//	public ResponseJson<PayInDetails> payInDetails(Integer page,Integer limit) {
//		ResponseJson<PayInDetails> responseJson=new ResponseJson<PayInDetails>();
//		try {
//			
//			PayIn payIn=new PayIn();
//			List<PayInDetails> details = payInService.findPayInDetails(payIn);
//			responseJson.setCode(0);
//			responseJson.setCount(details.size());
//			responseJson.setData(details);
//			return responseJson;
//		} catch (Exception e) {
//			e.printStackTrace();
//			responseJson.setCode(500);
//			responseJson.setMsg("系统维护中");
//			return responseJson;
//		}
//	}
	@RequestMapping("/payInSearchDetails")
	@ResponseBody
	/**
	 * 查询订单详情
	 */
	public ResponseJson<PayInDetails> payInSearchDetails(@RequestBody PayIn payIn) {
		ResponseJson<PayInDetails> responseJson=new ResponseJson<PayInDetails>();
		try {
			if(payIn.getNote()!=""&&payIn.getNote()!=null) {
				String[] split = payIn.getNote().split(" - ");
				payIn.setNote(split[0]);
				payIn.setDepId(split[1]);
			}
			List<PayInDetails> details = payInService.findPayInDetails(payIn);
			responseJson.setCode(0);
			responseJson.setCount(details.size());
			responseJson.setData(details);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(500);
			responseJson.setMsg("系统维护中");
			return responseJson;
		}
	}

	@RequestMapping("/payInDeleteDetails")
	@ResponseBody
	/**
	 *  删除订单
	 * @param typeid 项目编号
	 * @param inItemid 项目类型
	 * @param payInStatus 支付状态
	 */
	public ResponseInfo payInDeleteDetails(@PathParam("typeid") String typeid,@PathParam("inItemid") String inItemid,@PathParam("payInStatus")String payInStatus) {
		ResponseInfo responseInfo=new ResponseInfo(); 
		try {
			payInService.delPayInAndDetails(typeid, inItemid, payInStatus);
			responseInfo.setState(200);
			responseInfo.setMsg("删除成功");
			return responseInfo;
		} catch (Exception e) {
			e.printStackTrace();
			responseInfo.setState(500);
			responseInfo.setMsg("系统维护中");
			return responseInfo;
		}
	}
	
	@RequestMapping("/payInModifyDetails")
	@ResponseBody
	/**
	 * 修改订单
	 */
	public ResponseInfo payInModifyDetails(@RequestBody PayInDetails payInDetails) {
		ResponseInfo responseInfo=new ResponseInfo(); 
		try {
			payInService.modifyPayInAndDetails(payInDetails);
			responseInfo.setState(200);
			responseInfo.setMsg("添加成功");
			return responseInfo;
		} catch (Exception e) {
			e.printStackTrace();
			responseInfo.setState(500);
			responseInfo.setMsg("系统维护中");
			return responseInfo;
		}
	}
	
//	@RequestMapping("/depositList")
//	@ResponseBody
//	/**
//	 * 押金订单展示
//	 */
//	public ResponseJson<PayIn> depositList(Integer page,Integer limit) {
//		ResponseJson<PayIn> responseJson=new ResponseJson<PayIn>();
//		try {
//			PayIn payIn=new PayIn();
//			List<PayIn> payIns = payInService.findPayInDeposit(payIn, null, null);
//			responseJson.setCode(0);
//			responseJson.setCount(payIns.size());
//			responseJson.setData(payIns);
//			return responseJson;
//		} catch (Exception e) {
//			e.printStackTrace();
//			responseJson.setCode(500);
//			responseJson.setMsg("系统维护中");
//			return responseJson;
//		}
//	}
	
	@RequestMapping("/depositSearchDetails")
	@ResponseBody
	/**
	 * 查询押金订单
	 */
	public ResponseJson<PayIn> depositSearchDetails(@RequestBody PayIn payIn) {
		ResponseJson<PayIn> responseJson=new ResponseJson<PayIn>();
		Date begin=null;
		Date end=null;
		try {
			if(payIn.getNote()!=""&&payIn.getNote()!=null) {
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				String[] split = payIn.getNote().split(" - ");
				begin=sdf.parse(split[0]);
				end=sdf.parse(split[1]);
			}
			List<PayIn> payIns = payInService.findPayInDeposit(payIn, begin, end);
			responseJson.setCode(0);
			responseJson.setCount(payIns.size());
			responseJson.setData(payIns);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(500);
			responseJson.setMsg("系统维护中");
			return responseJson;
		}
	}
	
	
	@RequestMapping("/deletePayInDeposit")
	@ResponseBody
	/**
	 * 删除押金订单
	 */
	public ResponseInfo deletePayInDeposit(@PathParam("payInId") String payInId){
		ResponseInfo responseInfo=new ResponseInfo(); 
		try {
			payInService.delPayInById(payInId);
			responseInfo.setState(200);
			responseInfo.setMsg("删除成功");
			return responseInfo;
		} catch (Exception e) {
			e.printStackTrace();
			responseInfo.setState(500);
			responseInfo.setMsg("系统维护中");
			return responseInfo;
		}
	}
	
	
//	@RequestMapping("/outDetails")
//	@ResponseBody
//	/**
//	 * 门诊订单展示
//	 */
//	public ResponseJson<PayInDetails> outDetails(Integer page,Integer limit) {
//		ResponseJson<PayInDetails> responseJson=new ResponseJson<PayInDetails>();
//		try {
//			PayIn payIn=new PayIn();
//			List<PayInDetails> details = payInService.findPayInOut(payIn);
//			responseJson.setCode(0);
//			responseJson.setCount(details.size());
//			responseJson.setData(details);
//			return responseJson;
//		} catch (Exception e) {
//			e.printStackTrace();
//			responseJson.setCode(500);
//			responseJson.setMsg("系统维护中");
//			return responseJson;
//		}
//	}
	@RequestMapping("/outSearchDetails")
	@ResponseBody
	/**
	 * 查询门诊订单
	 */
	public ResponseJson<PayInDetails> outSearchDetails(@RequestBody PayIn payIn) {
		ResponseJson<PayInDetails> responseJson=new ResponseJson<PayInDetails>();
		try {
			if(payIn.getNote()!=""&&payIn.getNote()!=null) {
				String[] split = payIn.getNote().split(" - ");
				payIn.setNote(split[0]);
				payIn.setDepId(split[1]);
			}
			List<PayInDetails> details = payInService.findPayInOut(payIn);
			responseJson.setCode(0);
			responseJson.setCount(details.size());
			responseJson.setData(details);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(500);
			responseJson.setMsg("系统维护中");
			return responseJson;
		}
	}
	
	@RequestMapping("/payInModifyOut")
	@ResponseBody
	/**
	 * 修改门诊订单
	 */
	public ResponseInfo payInModifyOut(@RequestBody PayInDetails payInDetails) {
		ResponseInfo responseInfo=new ResponseInfo(); 
		try {
			payInService.modifyPayInAndOut(payInDetails);
			responseInfo.setState(200);
			responseInfo.setMsg("添加成功");
			return responseInfo;
		} catch (Exception e) {
			e.printStackTrace();
			responseInfo.setState(500);
			responseInfo.setMsg("系统维护中");
			return responseInfo;
		}
	}
}



